package apis

import (
	"fmt"
	"strings"
	"vrcm/src/vrcm/app/admin/service/dto/live_schedule"
	"vrcm/src/vrcm/app/admin/service/live_schedule"
	"vrcm/src/vrcm/common/apis"
	ext "vrcm/src/vrcm/envconfig"

	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
)

type SourceBasic struct {
	apis.Api
}

// GetPage 获取SourceBasic列表
// @Summary 获取SourceBasic列表
// @Description 获取SourceBasic列表
// @Tags SourceBasic
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.SourceBasic}} "{"code": 200, "data": [...]}"
// @Router /api/v1/source-basic [get]
// @Security Bearer
func (e SourceBasic) GetPage(c *gin.Context) {
	req := dto.SourceBasicGetPageReq{}
	s := service.SourceBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var count int64
	data, err := s.GetPage(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取信源失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取SourceBasic
// @Summary 获取SourceBasic
// @Description 获取SourceBasic
// @Tags SourceBasic
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.SourceBasic} "{"code": 200, "data": [...]}"
// @Router /api/v1/source-basic/{id} [get]
// @Security Bearer
func (e SourceBasic) Get(c *gin.Context) {
	req := dto.SourceBasicGetReq{}
	s := service.SourceBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.Get(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取信源失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// Insert 创建SourceBasic
// @Summary 创建SourceBasic
// @Description 创建SourceBasic
// @Tags SourceBasic
// @Accept application/json
// @Product application/json
// @Param data body dto.SourceBasicInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/source-basic [post]
// @Security Bearer
func (e SourceBasic) Insert(c *gin.Context) {
	req := dto.SourceBasicInsertReq{}
	s := service.SourceBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			e.Error(apis.RepeatError.Code, err, fmt.Sprintf("创建信源失败， %s", "信源名不能重复"))
		} else if strings.Contains(err.Error(), "流地址不能重复") {
			e.Error(apis.SourceRepeatError.Code, err, fmt.Sprintf("创建信源失败， %s", "流地址不能重复"))
		} else {
			e.Error(apis.AddSourceError.Code, err, fmt.Sprintf("创建信源失败，%s", err.Error()))
		}
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改SourceBasic
// @Summary 修改SourceBasic
// @Description 修改SourceBasic
// @Tags SourceBasic
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.SourceBasicUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/source-basic/{id} [put]
// @Security Bearer
func (e SourceBasic) Update(c *gin.Context) {
	req := dto.SourceBasicUpdateReq{}
	s := service.SourceBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			e.Error(apis.RepeatError.Code, err, fmt.Sprintf("修改信源失败， %s", "信源名不能重复"))
		} else if strings.Contains(err.Error(), "流地址不能重复") {
			e.Error(apis.SourceRepeatError.Code, err, fmt.Sprintf("修改信源失败， %s", "流地址不能重复"))
		} else {
			e.Error(apis.UpdateSourceError.Code, err, fmt.Sprintf("修改信源失败， %s", err.Error()))
		}

		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除SourceBasic
// @Summary 删除SourceBasic
// @Description 删除SourceBasic
// @Tags SourceBasic
// @Param data body dto.SourceBasicDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/source-basic [delete]
// @Security Bearer
func (e SourceBasic) Delete(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.SourceBasicDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除信源失败，%s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e SourceBasic) StartSource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.StartSourceReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StartSource(&req)
	if err != nil {
		e.Error(apis.StopSourceError.Code, err, fmt.Sprintf("开启信源失败，%s", err.Error()))
		return
	}
	e.OK("", "开启信源成功")
}

func (e SourceBasic) StopSource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.StopSourceReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StopSource(&req)
	if err != nil {
		e.Error(apis.StopSourceError.Code, err, fmt.Sprintf("关闭信源失败， %s", err.Error()))
		return
	}
	e.OK("", "关闭信源成功")
}

func (e SourceBasic) ChangeSource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.ChangeSourceReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.ChangeSource(&req)
	if err != nil {
		if err.Error() == string(apis.SourceChangeError.Code) {
			e.Error(apis.SourceChangeError.Code, err, fmt.Sprintf("切换信源失败， %s", "存在未填写的自定义转码流输出地址"))
		} else if err.Error() == string(apis.ShareSourceChangeError.Code) {
			e.Error(apis.ShareSourceChangeError.Code, err, fmt.Sprintf("切换信源失败， %s", "分享信源无法切换"))
		} else {
			e.Error(apis.ChangeSourceError.Code, err, fmt.Sprintf("切换信源失败， %s", err.Error()))
		}

		return
	}
	e.OK("", "切换信源成功")
}

func (e SourceBasic) SourceCallBack(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.SourceCallBackReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	e.Logger.Trace("进入信源回调", req)
	err = s.SourceCallBack(&req)
	if err != nil {
		e.Error(apis.CallBackError.Code, err, fmt.Sprintf("回调接口失败， %s", err.Error()))
		return
	}
	e.OK("", "回调接口成功")
}

func (e SourceBasic) GetShareSourceConfig(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetShareSourceConfig{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetShareSourceConfig(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取分享配置失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取分享配置成功")
}

func (e SourceBasic) GetShareSources(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetShareSourcesReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var count int64
	data, err := s.GetShareSources(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取分享列表失败， %s", err.Error()))
		return
	}
	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "获取分享列表成功")
}

func (e SourceBasic) UpdateShareSourceConfig(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.ShareSourceConfigUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.UpdateShareSourceConfig(&req)
	if err != nil {
		e.Error(apis.UpdateShareSourceConfigError.Code, err, fmt.Sprintf("更新分享信源配置失败， %s", err.Error()))
		return
	}
	e.OK("", "更新分享信源配置成功")
}

func (e SourceBasic) UpdateShareRemark(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.ShareSourceRemarkUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.UpdateShareRemark(&req)
	if err != nil {
		e.Error(apis.UpdateShareSourceConfigError.Code, err, fmt.Sprintf("更新分享信源配置失败， %s", err.Error()))
		return
	}
	e.OK("", "更新分享信源配置成功")
}

// 获取信源转码模版
func (e SourceBasic) SourcesTransCodeTaskPage(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetSourceTaskPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var count int64
	data, err := s.SourcesTransCodeTaskPage(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取信源转码模版失败， %s", err.Error()))
		return
	}
	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "获取信源转码模版成功")
}

// CreateSourceTransCodeTask 创建信源转码任务
func (e SourceBasic) CreateSourceTransCodeTask(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.CreateSourceTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	tx := e.Orm.Begin()
	err = s.CreateSourceTransCodeTask(tx, &req)
	if err != nil {
		tx.Rollback()
		e.Error(apis.CreateTransCodeTaskError.Code, err, fmt.Sprintf("创建联信源转码任务失败， %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK("", "创建联信源转码任务成功")
}

func (e SourceBasic) GetSwitchConfig(c *gin.Context) {
	s := service.SourceBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	isvdcm := 2
	if ext.ExtConfig.ServiceConfig.EnableSwitch == 1 {
		isvdcm = 1
	} else {
		isvdcm = 2
	}
	e.OK(isvdcm, "获取切换配置成功")
}

// StopSourceTransCode 停止信源转码任务
func (e SourceBasic) StopSourceTransCode(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.StopTransCodTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopSourceTransCodeTask(&req)
	if err != nil {
		e.Error(apis.StopSourceError.Code, err, fmt.Sprintf("停止联信源转码任务失败， %s", err.Error()))
		return
	}
	e.OK("", "停止联信源转码任务成功")
}

// DeleteSourceTransCode 删除信源转码任务
func (e SourceBasic) DeleteSourceTransCode(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.DeleteSourceTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteSourceTransCodeTask(&req)

	if err != nil {
		if strings.Contains(err.Error(), "转码任务正在被调度使用") {
			e.Error(apis.TransCodeIsUsing.Code, err, fmt.Sprintf("删除信源转码任务失败，%s", err.Error()))
			return
		}
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除信源转码任务失败，%s", err.Error()))
		return
	}
	e.OK("", "删除联信源转码任务成功")
}

func (e SourceBasic) GetSourcesStatistics(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetSourcesStatistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetSourcesStatistics(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取信源信息统计失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取信源信息统计成功")
}

func (e SourceBasic) TransCodeCallBack(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.TransCodeCallBackReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	e.Logger.Trace("转码回调", req)
	err = s.TransCodeCallBack(&req)
	if err != nil {
		e.Error(apis.CallBackError.Code, err, fmt.Sprintf("回调接口失败， %s", err.Error()))
		return
	}
	e.OK("", "回调接口成功")
}

func (e SourceBasic) StartSourceRecord(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.StartSourceRecordReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StartSourceRecord(&req)
	if err != nil {
		e.Error(apis.StartSourceRecordError.Code, err, fmt.Sprintf("开启信源录制任务失败， %s", err.Error()))
		return
	}
	e.OK("", "开启信源录制任务成功")
}

func (e SourceBasic) StopSourceRecord(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.StartSourceRecordReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StopSourceRecord(&req)
	if err != nil {
		e.Error(apis.StartSourceRecordError.Code, err, fmt.Sprintf("停止信源录制任务失败， %s", err.Error()))
		return
	}
	e.OK("", "停止信源录制任务成功")
}

func (e SourceBasic) RecordFileCallBack(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.RecordFileCallBack{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	e.Logger.Trace("信源录制文件回调", req)
	err = s.RecordFileCallBack(&req)
	if err != nil {
		e.Error(apis.CallBackError.Code, err, fmt.Sprintf("信源录制回调失败， %s", err.Error()))
		return
	}
	e.OK("", "信源录制回调成功")
}

func (e SourceBasic) GetSourceRecordPage(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.SourceRecordGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var count int64
	data, err := s.GetSourceRecordPage(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("查询录制失败， %s", err.Error()))
		return
	}
	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")

}

func (e SourceBasic) GetSourceRecord(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetSourceRecordReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetSourceRecord(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("查询录制失败， %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")

}

func (e SourceBasic) DelSourceRecord(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.SourceRecordDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteSourceRecord(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("删除录制文件失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除录制文件成功")

}

func (e SourceBasic) CheckDelSource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.SourceBasicDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.CheckDelSource(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("删除校验失败，%s", err.Error()))
		return
	}

	e.OK(data, "查询成功")

}

func (e SourceBasic) CheckDelTransSource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.SourceTransDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.CheckDelTransSource(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("删除校验失败，%s", err.Error()))
		return
	}
	e.OK(data, "查询成功")

}

func (e SourceBasic) StartTransCode(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.StarTransCodTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StartSourceTransCodeTask(&req)
	if err != nil {
		e.Error(apis.StartTransCodeTaskError.Code, err, fmt.Sprintf("开始转码失败，%s", err.Error()))
		return
	}

	e.OK(nil, "开始转码成功")

}

func (e SourceBasic) UpdateTransCode(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.UpdateTransCodeTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.UpdateSourceTransCodeTask(&req)
	if err != nil {
		e.Error(apis.UpdateTransCodeTaskError.Code, err, fmt.Sprintf("更新转码失败，%s", err.Error()))
		return
	}

	e.OK(nil, "更新转码成功")

}

func (e SourceBasic) GetAuditStatisticBySource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetAuditResultBySourceReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetAuditStatisticBySource(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取信源审核数据失败，%s", err.Error()))
		return
	}

	e.OK(data, "获取信源审核数据成功")
}
func (e SourceBasic) GetAuditStatisticByType(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetAuditResultDetailBySourceReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var count int64
	data, err := s.GetAuditStatisticByType(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取信源审核数据失败，%s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.PageIndex, req.PageSize, "获取信源审核数据成功")
}

func (e SourceBasic) UpdateAuditState(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.UpdateAuditState{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.UpdateAuditState(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("更新审核状态失败，%s", err.Error()))
		return
	}
	e.OK(nil, "更新审核状态成功")
}

func (e SourceBasic) GetAuditHistoryBySource(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.GetAuditState{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetAuditHistoryBySource(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取审核历史失败，%s", err.Error()))
		return
	}
	e.OK(data, "获取审核历史成功")
}

func (e SourceBasic) AutoSwitchSourceCallBack(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.AutoSwitchReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	e.Logger.Trace("进入信源自动切换", req)
	err = s.AutoSwitchSource(&req)
	if err != nil {
		e.Error(apis.AutoSwitchSourceError.Code, err, fmt.Sprintf("自动切换信源失败， %s", err.Error()))
		return
	}
	e.OK("", "自动切换信源成功")
}

func (e SourceBasic) EquipBeat(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.EquipBeatReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.EquipBeat(&req)
	if err != nil {
		e.Error(apis.AutoSwitchSourceError.Code, err, fmt.Sprintf("设备心跳失败， %s", err.Error()))
		return
	}
	e.OK("", "设备心跳成功")
}

func (e SourceBasic) SaveSourceBasicNew(c *gin.Context) {
	s := service.SourceBasicService{}
	req := dto.EquipBeatReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.EquipBeat(&req)
	if err != nil {
		e.Error(apis.AutoSwitchSourceError.Code, err, fmt.Sprintf("设备心跳失败， %s", err.Error()))
		return
	}
	e.OK("", "设备心跳成功")
}
